package com.jbit.service.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbit.mapper.EmployeeMapper;
import com.jbit.mapper.MailLogMapper;
import com.jbit.pojo.Employee;
import com.jbit.pojo.MailLog;
import com.jbit.pojo.common.RespBean;
import com.jbit.pojo.common.RespPageBean;
import com.jbit.pojo.constant.MailConstant;
import com.jbit.service.IEmployeeService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lirenzhi
 * @since 2021-06-24
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;

    /**
     * 分页显示员工信息
     *
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        //开启分页
        Page<Employee> page = new Page<>(currentPage, size);
        IPage<Employee> employeeIPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        return new RespPageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
    }

    /**
     * 获取最大工号
     *
     * @return
     */
    @Override
    public RespBean getMaxWorkID() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>().select("max(workID)");
        List<Map<String, Object>> maps = employeeMapper.selectMaps(wrapper);
        Integer maxWorkID = Integer.parseInt(maps.get(0).get("max(workID)").toString()) + 1;
        //防止乱码  一共八位  前面不足的按0补齐
        String formatMaxWorkID = String.format("%08d", maxWorkID);
        return RespBean.success(null, formatMaxWorkID);
    }

    /**
     * 添加员工  成功之后发送邮件
     *
     * @param employee
     */
    @Override
    public RespBean addEmp(Employee employee) {
        //处理合同期限的计算
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        //相差的多少天
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        //保留两位小数
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));

        if (employeeMapper.insert(employee) == 1) {
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            //消息落库
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstant.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstant.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstant.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);

            rabbitTemplate.convertAndSend(MailConstant.MAIL_EXCHANGE_NAME, MailConstant.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(msgId));
            return RespBean.success("插入成功！");
        }
        return RespBean.error("插入失败！");
    }

    /**
     * 查询员工  不传参数，查所有
     *
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    /**
     * 获取所有员工工资账套
     *
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeWithSalary = employeeMapper.getEmployeeWithSalary(page);
        return new RespPageBean(employeeWithSalary.getTotal(), employeeWithSalary.getRecords());
    }

    /**
     * 批量导出
     * @param ids
     * @return
     */
    @Override
    public List<Employee> getEmployeeBatch(Integer[] ids) {
        return employeeMapper.getEmployeeBatch(ids);
    }
}
